package rs.fon.is.music.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.log4j.Logger;

public class Settings {

	private static Logger logger = Logger.getLogger(Settings.class);

	public static String contextPath;
	private static final String configFilePath = "config" + File.separator + "config.xml";

	public Config config;
	
	private static class SettingsHolder { 
		private static final Settings INSTANCE = new Settings();
	}

	public static Settings getInstance() {
		return SettingsHolder.INSTANCE;
	}
	
	private Settings() {
		try {
			loadConfig();
		} catch (Exception e) {
			logger.error("Could not load settings: ", e);
		}
	}

	private void loadConfig() throws Exception {
		String pathname = contextPath + File.separator + configFilePath;
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(Config.class);
			Unmarshaller um = jaxbContext.createUnmarshaller();
			File configFile = new File(pathname);
			this.config = (Config) um.unmarshal(new FileReader(configFile));
		} catch (JAXBException e) {
			logger.error("Error reading config file contents: "+pathname+". "+e);
		} catch (FileNotFoundException e) {
			logger.error("Could not find a config file at the location: "+pathname+". "+e);
		}
	}

}
